import 'package:pacsun/model/sysUserTagModel.dart' show YouAreTagModel;

class UserModel {
  final int? id;
  final String? aid;
  final String? nickName;
  final String? imageUrl;
  final int? createTime;
  final int? updateTime;
  final String? fullName;
  final String? email;
  final String? address;
  final dynamic socialAccounts;
  final int? userType; // 0 = common user; 1 = creator
  final String? bio;
  final int? taskCountId;
  final String? inviterAid;
  final String? firstName;
  final String? lastName;
  final String? password;
  final String? confirmPassword;
  final String? phone;
  final String? birthday;
  final String? shoppingFor;
  final int? mediaType;
  final int? deleted;
  final int? status;
  final String? createUser;
  final String? updateUser;
  final int? followers;
  final String? youAre;
  final String? openId;
  final Map<String, dynamic>? connected;
//  List<YouAreTagModel>? tags;

  UserModel({
    this.id,
    this.aid,
    this.nickName,
    this.imageUrl,
    this.createTime,
    this.updateTime,
    this.fullName,
    this.email,
    this.address,
    this.socialAccounts,
    this.userType,
    this.bio,
    this.taskCountId,
    this.inviterAid,
    this.firstName,
    this.lastName,
    this.password,
    this.confirmPassword,
    this.phone,
    this.birthday,
    this.shoppingFor,
    this.mediaType,
    this.deleted,
    this.status,
    this.createUser,
    this.updateUser,
    this.followers,
    this.youAre, // this is current tag
    this.connected,
    this.openId,
  });

  // CopyWith method
  UserModel copyWith({
    int? id,
    String? aid,
    String? nickName,
    String? imageUrl,
    int? createTime,
    int? updateTime,
    String? fullName,
    String? email,
    String? address,
    dynamic socialAccounts,
    int? userType,
    String? bio,
    int? taskCountId,
    String? inviterAid,
    String? firstName,
    String? lastName,
    String? password,
    String? confirmPassword,
    String? phone,
    String? birthday,
    String? shoppingFor,
    int? mediaType,
    int? deleted,
    int? status,
    String? createUser,
    String? updateUser,
    int? followers,
    String? youAre, // tags
    Map<String, dynamic>? connected,
    String? openId,
  }) {
    return UserModel(
        id: id ?? this.id,
        aid: aid ?? this.aid,
        nickName: nickName ?? this.nickName,
        imageUrl: imageUrl ?? this.imageUrl,
        createTime: createTime ?? this.createTime,
        updateTime: updateTime ?? this.updateTime,
        fullName: fullName ?? this.fullName,
        email: email ?? this.email,
        address: address ?? this.address,
        socialAccounts: socialAccounts ?? this.socialAccounts,
        userType: userType ?? this.userType,
        bio: bio ?? this.bio,
        taskCountId: taskCountId ?? this.taskCountId,
        inviterAid: inviterAid ?? this.inviterAid,
        firstName: firstName ?? this.firstName,
        lastName: lastName ?? this.lastName,
        password: password ?? this.password,
        confirmPassword: confirmPassword ?? this.confirmPassword,
        phone: phone ?? this.phone,
        birthday: birthday ?? this.birthday,
        shoppingFor: shoppingFor ?? this.shoppingFor,
        mediaType: mediaType ?? this.mediaType,
        deleted: deleted ?? this.deleted,
        status: status ?? this.status,
        createUser: createUser ?? this.createUser,
        updateUser: updateUser ?? this.updateUser,
        followers: followers ?? this.followers,
        youAre: youAre ?? this.youAre,
        connected: connected ?? this.connected,
        openId: openId ?? this.openId
        // tags: tags ?? this.tags,
        );
  }

  @override
  String toString() {
    return 'UserModel{'
        'id: $id, '
        'aid: $aid, '
        'nickName: $nickName, '
        'imageUrl: $imageUrl, '
        'createTime: $createTime, '
        'updateTime: $updateTime, '
        'fullName: $fullName, '
        'email: $email, '
        'address: $address, '
        'socialAccounts: $socialAccounts, '
        'userType: $userType, '
        'bio: $bio, '
        'taskCountId: $taskCountId, '
        'inviterAid: $inviterAid, '
        'firstName: $firstName, '
        'lastName: $lastName, '
        'password: ${password != null ? '[hidden]' : 'null'}, '
        'confirmPassword: ${confirmPassword != null ? '[hidden]' : 'null'}, '
        'phone: $phone, '
        'birthday: $birthday, '
        'shoppingFor: $shoppingFor, '
        'mediaType: $mediaType, '
        'deleted: $deleted, '
        'status: $status, '
        'createUser: $createUser, '
        'updateUser: $updateUser, '
        'followers: $followers, '
        'youAre: $youAre, '
        'connected: $connected, '
        'openId:  $openId, '
        '}';
  }

  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['id'] ?? 0,
      aid: json['aid'] ?? '',
      nickName: json['nickName'],
      imageUrl: json['imageUrl'],
      createTime: json['createTime'],
      updateTime: json['updateTime'],
      fullName: json['fullName'],
      email: json['email'],
      address: json['address'],
      socialAccounts: json['socialAccounts'],
      userType: json['userType'] ?? 0,
      bio: json['bio'],
      taskCountId: json['taskCountId'],
      inviterAid: json['inviterAid'],
      firstName: json['firstName'],
      lastName: json['lastName'],
      password: json['password'],
      confirmPassword: json['confirmPassword'],
      phone: json['phone'],
      birthday: json['birthday'],
      shoppingFor: json['shoppingFor'],
      mediaType: json['mediaType'],
      deleted: json['deleted'],
      status: json['status'],
      createUser: json['createUser'],
      updateUser: json['updateUser'],
      followers: json['followers'],
      youAre: json['youAre'],
      connected: json['connected'] != null
          ? Map<String, dynamic>.from(json['connected'])
          : null,
      openId: json['openId'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'aid': aid,
      'nickName': nickName,
      'imageUrl': imageUrl,
      'createTime': createTime,
      'updateTime': updateTime,
      'fullName': fullName,
      'email': email,
      'address': address,
      'socialAccounts': socialAccounts,
      'userType': userType,
      'bio': bio,
      'taskCountId': taskCountId,
      'inviterAid': inviterAid,
      'firstName': firstName,
      'lastName': lastName,
      'password': password,
      'confirmPassword': confirmPassword,
      'phone': phone,
      'birthday': birthday,
      'shoppingFor': shoppingFor,
      'mediaType': mediaType,
      'deleted': deleted,
      'status': status,
      'createUser': createUser,
      'updateUser': updateUser,
      'followers': followers,
      'youAre': youAre,
      'connected': connected,
      'openId': openId
    };
  }

  // Helper methods for timestamps
  DateTime? get createDate => createTime != null
      ? DateTime.fromMillisecondsSinceEpoch(createTime!)
      : null;

  DateTime? get updateDate => updateTime != null
      ? DateTime.fromMillisecondsSinceEpoch(updateTime!)
      : null;
}
